<template>
  <div class="page">
    <div v-if="!detail.visible">
      <nav class="nav">
        <div class="left_area">
          <el-button-group>
            <el-button type="info"
            >每期排行数量 {{ meta.header && meta.header.number }}
            </el-button
            >
            <el-button
              v-for="item in meta.header.types"
              :key="item"
              type="danger"
              v-text="`奖池 ${item}`"
            ></el-button>
          </el-button-group>
        </div>

        <div class="right_area">
          <el-button plain type="success" @click="handleAddNew">新增</el-button>
          <el-button plain type="info" @click="handlePoolConfig"
          >奖池配置
          </el-button
          >
        </div>
      </nav>
      <el-table v-loading="loading" :data="dataList" border height="70vh">
        <el-table-column label="昵称" prop="nickname"></el-table-column>
        <el-table-column label="头像" prop="head_picture">
          <template slot-scope="{ row }">
            <el-avatar
              :src="row.head_picture"
              shape="square"
              size="large"
            ></el-avatar>
          </template>
        </el-table-column>
        <el-table-column :width="200" label="添加时间" prop="add_time">
          <template slot-scope="{ row }">
            <el-tag
              v-text="new Date(row.add_time * 1000).toLocaleString()"
            ></el-tag>
          </template>
        </el-table-column>
        <el-table-column :width="150" label="过期时间" prop="expired_time">
          <template slot-scope="{ row }">
            <el-tag
              v-text="new Date(row.expired_time * 1000).toLocaleString()"
            ></el-tag>
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="status">
          <template slot-scope="{ row }">
            <el-tag
              :type="row.status === 1 ? 'success' : 'danger'"
              v-text="row.status === 1 ? '有效' : '无效'"
            ></el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="特殊奖池累计投入"
          prop="total_cost"
        ></el-table-column>
        <el-table-column
          label="特殊奖池累计产出"
          prop="total_income"
        ></el-table-column>
        <!--        <el-table-column label="详情">-->
        <!--          <template slot-scope="{ row }">-->
        <!--            <el-button plain @click="handleDetail(row)">查看详情</el-button>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <el-button plain @click="handleEditForm(row)">编辑</el-button>
            <el-button plain type="danger" @click="handleDeleteRow(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :current-page.sync="currentPage"
        :page-size="pagination.limit"
        :total="pagination.total"
        background
        hide-on-single-page
        layout="prev,pager, next"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>

    <userDetails v-if="detail.visible" :userId="detail.user_id" @updateVisible="detail.visible = false"/>

    <el-dialog
      :title="currentDialogStatus"
      :visible.sync="visibleEdit"
      @closed="handleDialogClose"
    >
      <el-form ref="dataForm" v-model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户编号" prop="user_code">
          <el-input v-model="form.user_code" style="width:220px"></el-input>
        </el-form-item>
        <el-form-item label="过期时间" prop="expired_time">
          <el-date-picker
            v-model="form.expired_time"
            placeholder="选择日期时间"
            type="datetime"
            value-format="timestamp"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>

      <div slot="footer">
        <el-button plain @click="handleCancel">取消</el-button>
        <el-button plain @click="handleSubmit">提交</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="visiblePoolConfig" title="奖池设置">
      <el-form ref="poolConfig" v-model="filterForm" label-width="120px">
        <el-form-item label="数量" prop="size">
          <el-input-number
            v-model="filterForm.number"
            :min="1"
            style="display: inline-block"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="奖池" prop="prizePool">
          <el-checkbox-group v-model="filterForm.types">
            <el-checkbox-button v-for="item in 5" :key="item" :label="item"
            >奖池{{ item }}
            </el-checkbox-button
            >
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <template slot="footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button @click="handleConfirm">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  alterOrAddPersonalUser,
  delPersonalUser,
  getSpecialUserList,
  toPersonalSetConfig
} from '@/api/luckyTreeGame/luckyTreeGame'

import userDetails from '@/views/personalSpecialPrizePool/specialUserList/childComps/userDetails'

export default {
  name: 'specialUserList',
  data() {
    return {
      detail: {
        visible: false,
        user_id: null
      },
      loading: true,
      dataList: [], // 数据列表
      currentDialogStatus: '', // 编辑  新增
      visibleEdit: false,
      visiblePoolConfig: false, // 奖池配置
      form: {
        id: undefined,
        user_code: 1,
        expired_time: undefined
      },
      rules: {},
      meta: {
        header: { types: [] }
      },
      pagination: {},
      currentPage: 1,
      filterForm: {
        types: [],
        number: 1
      }
    }
  },
  components: { userDetails },
  mounted() {
    this.refreshData()
  },
  methods: {
    handleDetail(row) {
      this.detail.user_id = row.user_id
      this.detail.visible = true
    },
    async refreshData(page) {
      this.loading = true
      const { data } = await getSpecialUserList(page)
      this.dataList = data.list
      this.filterForm.types = data.header.types.map(item => parseInt(item))
      this.filterForm.number = parseInt(data.header.number)
      this.pagination = data.meta
      this.meta = data
      this.loading = false
    },
    async handleDeleteRow(rowData) {
      const { code } = await delPersonalUser(rowData.id)
      if (code === 200) {
        this.$message.info('删除成功')
        await this.refreshData()
      }
    },
    handleEditForm(rowData) {
      this.currentDialogStatus = '编辑'
      this.visibleEdit = true

      this.form.id = rowData.id
      this.form.user_code = rowData.user_code
      this.form.expired_time = rowData.expired_time * 1000
    },
    handleDialogClose() {
      this.form.id = undefined
      this.form.user_code = undefined
      this.form.expired_time = undefined
    },
    handleCancel() {
      this.visibleEdit = false
      this.visiblePoolConfig = false
      this.handleDialogClose()
    },
    handleAddNew() {
      /*
       * 单机新增按钮
       * */
      this.currentDialogStatus = '新增'
      this.form.type = 30
      this.visibleEdit = true
    },
    async handleSubmit() {
      this.form.expired_time = this.form.expired_time / 1000
      if (this.currentDialogStatus === '编辑') {
        const { code } = await alterOrAddPersonalUser(this.form)
        if (code === 200) {
          this.$message.success('修改成功！')
          await this.refreshData()
        }
      } else if (this.currentDialogStatus === '新增') {
        const { code } = await alterOrAddPersonalUser({
          user_code: this.form.user_code,
          expired_time: this.form.expired_time
        })
        if (code === 200) {
          this.$message.success('添加成功！')
          await this.refreshData()
        }
      }
      this.visibleEdit = false
      this.form.expired_time = this.form.expired_time * 1000
    },
    handleCurrentChange(pageIndex) {
      this.currentPage = pageIndex
      this.refreshData(pageIndex)
    },
    handlePoolConfig() {
      /*
       * 单机奖池配置
       * */
      this.visiblePoolConfig = true
    },
    async handleConfirm() {
      /*
       * 筛选提交
       * */
      const data = toPersonalSetConfig(this.filterForm)
      await this.refreshData()
      this.visiblePoolConfig = false
    }
  }
}
</script>

<style lang="scss" scoped>
.page {
  box-sizing : border-box;
  padding    : 10px;
  margin     : 10px;

  .nav {
    margin-bottom   : 10px;
    padding         : 10px 5px;
    box-shadow      : 0 0 15px 0 rgba(0, 0, 0, 0.3);
    border-radius   : 5px;

    display         : flex;
    justify-content : space-between;
  }

  .el-image {
    width  : 80px;
    height : 80px;
  }

  .el-pagination {
    text-align : right;
    margin-top : 20px;
  }

  &::v-deep .el-dialog__body {
    border : none;
  }
}
</style>
